package yuzhou;


import com.sun.jna.*;

import java.io.IOException;
import java.util.List;
import java.util.UUID;


public class Yz {
    private static Integer dwEntityList = 0;
    private static Integer dwGlowObjectManager = 0;
    //private static Integer dwLocalPlayer = 0xD882BC;

    private final static Integer m_iGlowIndex = 0xA438;
    //private final static Integer m_iTeamNum = 0xF4;
    //private final static Integer m_iHealth = 0x100;
    //private final static Integer m_bDormant = 0xED;

    private static LPMODULEENTRY32 client;



    public static void main(String[] args) throws IOException {
        if(!Platform.isWindows()) {
            System.err.println("请在Windows系统运行!");
            return;
        }
        System.setProperty("jna.encoding", "GBK");

        Kernel32.INSTANCE.SetConsoleTitleA(UUID.randomUUID().toString());

        Integer hwnd = User32.INSTANCE.FindWindowA("Valve001",null);
        if(hwnd == 0)return;

        Pointer pid = new Memory(4);
        User32.INSTANCE.GetWindowThreadProcessId(hwnd,pid);

        Integer hProcess = Kernel32.INSTANCE.OpenProcess(2035711,0,pid.getInt(0));
        //pid
        //System.out.println("PID:"+pid.getInt(0)+"\n进程句柄:"+hProcess);


        /*//读内存
        System.out.println(readMem(hProcess,0x2326C24).getInt(0));

        //写内存
        Pointer data = new Memory(4);
        data.setInt(0,1);
        writeMem(hProcess,0x2326C24,data);*/

        for (LPMODULEENTRY32 i:Mem.getModule(pid.getInt(0))){
            if("client.dll".equals(i.szname)){
                client = i;
                break;
            }
        }

        String EntityList = Config.readConfig(Config.ENTITYLIST);
        String GlowObj = Config.readConfig(Config.GlOWOBJ);
        if (EntityList == null||GlowObj==null){
            System.out.println("智能寻址，初始化....");
            Integer address = client.base;
            Integer qureyLen = client.base+client.bsize;

            String hex = "BB ?? ?? ?? ?? 83 FF 01 0F 8C ?? ?? ?? ?? 3B F8";
            String hex2 = "A1 ?? ?? ?? ?? A8 01 75 4B";

            List<Integer> list = Mem.findMem(hProcess,address,qureyLen,hex);
            List<Integer> list2 = Mem.findMem(hProcess,address,qureyLen,hex2);

            Integer addr = list.get(0);
            Integer addr2 = list2.get(0);
            dwEntityList = Mem.readMem(hProcess,addr+1).getInt(0);
            dwGlowObjectManager = Mem.readMem(hProcess,addr2+1).getInt(0)+4;
            Config.writeConfig(Config.ENTITYLIST,dwEntityList.toString());
            Config.writeConfig(Config.GlOWOBJ,dwGlowObjectManager.toString());
            System.out.println("初始化完毕,下次无需初始化");
        }else{
            dwEntityList = Integer.parseInt(EntityList);
            dwGlowObjectManager = Integer.parseInt(GlowObj);
        }

        //System.out.println(Integer.toHexString(dwEntityList));
        //System.out.println(Integer.toHexString(dwGlowObjectManager));

        Pointer red = new Memory(4);
        Pointer green = new Memory(4);
        Pointer blue = new Memory(4);
        Pointer alfa = new Memory(4);
        Pointer t = new Memory(1);

        red.setFloat(0,1);
        green.setFloat(0,0);
        blue.setFloat(0,0);
        alfa.setFloat(0,1);
        t.setByte(0, (byte) 1);

        while (true){
            Pointer glowManager = Mem.readMem(hProcess,dwGlowObjectManager);

            for (int i=0;i<32;i++){
                Pointer entity = Mem.readMem(hProcess,dwEntityList+i*0x10);
                if(entity.getInt(0)==0)continue;
                //Pointer teamId = Mem.readMem(hProcess,entity.getInt(0)+m_iTeamNum);
                //if(teamId.getInt(0) != 2 || teamId.getInt(0) !=3)continue;
                Pointer glow = Mem.readMem(hProcess,entity.getInt(0)+m_iGlowIndex);
                Mem.writeMem(hProcess,(glowManager.getInt(0) + ((glow.getInt(0) * 0x38) + 0x4)),red);
                Mem.writeMem(hProcess,(glowManager.getInt(0) + ((glow.getInt(0) * 0x38) + 0x8)),green);
                Mem.writeMem(hProcess,(glowManager.getInt(0) + ((glow.getInt(0) * 0x38) + 0xC)),blue);
                Mem.writeMem(hProcess,(glowManager.getInt(0) + ((glow.getInt(0) * 0x38) + 0x10)),alfa);
                Mem.writeMemBool(hProcess,(glowManager.getInt(0) + ((glow.getInt(0) * 0x38) + 0x24)),t);
            }
            Kernel32.INSTANCE.Sleep(1);
        }

        //Kernel32.INSTANCE.CloseHandle(hProcess);
    }
}
